use flate2::{write::GzEncoder, read::GzDecoder, Compression};
use std::io::prelude::*;
use crate::remote::errors::CompressionError;

pub struct CompressionHandler {
    level: u32,
}

impl CompressionHandler {
    pub fn new(level: u32) -> Self {
        Self { level }
    }

    pub fn compress(&self, data: &[u8]) -> Result<Vec<u8>, CompressionError> {
        let mut encoder = GzEncoder::new(Vec::new(), Compression::new(self.level));
        encoder.write_all(data)
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))?;
        encoder.finish()
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))
    }

    pub fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, CompressionError> {
        let mut decoder = GzDecoder::new(data);
        let mut decompressed = Vec::new();
        decoder.read_to_end(&mut decompressed)
            .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))?;
        Ok(decompressed)
    }
} 